package com.toman.ui.work.base;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.toman.Adpter.ItemTypeAdapter;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.DeviceTypeEntity;
import com.toman.R;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.bean.ItemTypeBean;
import com.toman.toolbar.ToolBarActivity;
import com.toman.widget.MyListView;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

public class DeviceTypeInfActivity extends ToolBarActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_no)
    TextView tvTitleNo;
    @BindView(R.id.tv_title2)
    TextView tvTitle2;
    @BindView(R.id.tv_title3)
    TextView tvTitle3;
    @BindView(R.id.tv_title4)
    TextView tvTitle4;

    @BindView(R.id.cv_base)
    CardView cvBase;
    @BindView(R.id.lv_dj)
    MyListView lvDj;
    @BindView(R.id.cv_more)
    CardView cvMore;
    @BindView(R.id.lv_ywh)
    MyListView lvYwh;
    @BindView(R.id.lv_whjc)
    MyListView lvWhjc;
    ItemTypeAdapter adapter1, adapter2, adapter3;
    List<ItemTypeBean> list1 = new ArrayList<ItemTypeBean>();
    List<ItemTypeBean> list2 = new ArrayList<ItemTypeBean>();
    List<ItemTypeBean> list3 = new ArrayList<ItemTypeBean>();
    @BindView(R.id.tv_content1)
    TextView tvContent1;
    @BindView(R.id.tv_content2)
    TextView tvContent2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_type_inf);
        ButterKnife.bind(this);
        initView();
        initToolBar("设备类别详情");
        get();
    }

    private void initView() {
        id = getIntent().getStringExtra("id");
        adapter1 = new ItemTypeAdapter(this,3);
        adapter2 = new ItemTypeAdapter(this,3);
        adapter3 = new ItemTypeAdapter(this,3);
        lvDj.setAdapter(adapter1);
        lvDj.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ItemTypeBean bean=adapter1.getItem(i);
message(1,bean);
            }
        });
        lvYwh.setAdapter(adapter2);
        lvYwh.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ItemTypeBean bean=adapter2.getItem(i);
                message(2,bean);
            }
        });
        lvWhjc.setAdapter(adapter3);
        lvWhjc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ItemTypeBean bean=adapter3.getItem(i);
                message(2,bean);

            }
        });
    }


    public void get() {
        OkHttpUtils
                .get(NewBaseUrl.devicetype_info_get + id)
                .params("token", token)
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", ": 设备类别详情" + result);
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());
                        if (code == 0) {
                            DeviceTypeEntity bean = (DeviceTypeEntity) JSON.parseObject(jsonObj.getString("deviceType"), DeviceTypeEntity.class);
//
                            tvTitle.setText(bean.getDeviceTypeName());
                            tvTitleNo.setText(bean.getDeviceTypeCode());
                            tvTitle2.setText(bean.getProtectRunningTime());
                            tvTitle3.setText(bean.getProtectNaturalTime());
                            tvTitle4.setText(bean.getStatus().equals("0")? "正常":"停用");

                            tvContent1.setText(bean.getRemark());
                            tvContent2.setText(bean.getUpdateTime());
                            JSONObject Jpage = jsonObj.getJSONObject("deviceType");
                            try {
                                JSONArray jsonArray1 = Jpage.getJSONArray("checkItemList");
                                for (int i=0;i<jsonArray1.size();i++){
                                    ItemTypeBean bean1 =new ItemTypeBean();
                                   JSONObject sa = jsonArray1.getJSONObject(i);
                                    bean1.setName(sa.getString("dictValue"));
                                    bean1.setNo(sa.getString("dictLabel"));
                                    bean1.setContent1(sa.getString("field1"));
                                    bean1.setContent2(sa.getString("field2Name"));
                                    bean1.setRemark(sa.getString("remark"));
                                    bean1.setStatus(sa.getString("dataStatus"));
                                    list1.add(bean1);
                                }
                                adapter1.refreshData(list1);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            try {
                                JSONArray jsonArray2 = Jpage.getJSONArray("preMaintenanceProjectList");

                                for (int i=0;i<jsonArray2.size();i++){
                                    ItemTypeBean bean1 =new ItemTypeBean();
                                    JSONObject sa = jsonArray2.getJSONObject(i);
                                    bean1.setName(sa.getString("dictValue"));
                                    bean1.setNo(sa.getString("dictLabel"));
                                    bean1.setRemark(sa.getString("remark"));
                                    bean1.setStatus(sa.getString("dataStatus"));
                                    list2.add(bean1);
                                }
                                adapter2.refreshData(list2);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            try {
                                JSONArray jsonArray3 = Jpage.getJSONArray("completeEquipmentInspectionProjectList");

                                for (int i = 0; i < jsonArray3.size(); i++) {
                                    ItemTypeBean bean1 = new ItemTypeBean();
                                    JSONObject sa = jsonArray3.getJSONObject(i);
                                    bean1.setName(sa.getString("dictValue"));
                                    bean1.setNo(sa.getString("dictLabel"));
                                    bean1.setRemark(sa.getString("remark"));
                                    bean1.setStatus(sa.getString("dataStatus"));
                                    list3.add(bean1);
                                }
                                adapter3.refreshData(list3);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }


                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }

                    }
                });
    }

    public void message(int type, ItemTypeBean bean) {
        final AlertDialog dialog;
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater factory = LayoutInflater.from(this);
        //把activity_login中的控件定义在View中
         View v = factory.inflate(R.layout.dialog_item_type, null);

        TextView content1= (TextView) v.findViewById(R.id.tv_content1);
        TextView content2= (TextView) v.findViewById(R.id.tv_content2);
        TextView content3= (TextView) v.findViewById(R.id.tv_content3);
        TextView content4= (TextView) v.findViewById(R.id.tv_content4);
        TextView content5= (TextView) v.findViewById(R.id.tv_content5);
        TextView content6= (TextView) v.findViewById(R.id.tv_content6);
        RelativeLayout rl3= (RelativeLayout) v.findViewById(R.id.rl3);
        RelativeLayout rl4= (RelativeLayout) v.findViewById(R.id.rl4);
content1.setText(bean.getNo());
content2.setText(bean.getName());

content5.setText(bean.getRemark());
content6.setText(bean.getStatus().equals("0")? "正常":"停用");


if (type==1){
    content3.setText(bean.getContent1());
    content4.setText(bean.getContent2());
}
else {
    rl3.setVisibility(View.GONE);
    rl4.setVisibility(View.GONE);

}

        builder                //对话框的标题
                .setTitle(bean.getName())
                //设定显示的View
                .setView(v)
                //对话框中的“登陆”按钮的点击事件
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {



                    }
                })
                //对话框的“退出”单击事件
//                .setNegativeButton("退出", new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int whichButton) {
//                    }
//                })
                // 设置dialog是否为模态，false表示模态，true表示非模态
                .setCancelable(true)
                //对话框的创建、显示
                .create();
        dialog = builder.show();

    }
}
